package com.beidu.lottery.call.service.portal.validator.impl;

import com.beidu.lottery.call.constant.ActivityTypeEnum;
import com.beidu.lottery.call.entity.PortalActivityRule;
import com.beidu.lottery.call.exception.ServiceException;
import com.beidu.lottery.call.service.portal.ActivityRegService;
import com.beidu.lottery.call.service.portal.validator.AbstractValidator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * 报名规则校验 实现类
 *
 * @author 北渡
 */
@Slf4j
@Service
@SuppressWarnings("unused")
public class RegValidator extends AbstractValidator {

    /**
     * 需要执行的校验函数
     * <p>
     * TODO 扩展
     */
    private final List<String> methods = Arrays.asList(
            "activityDateValidate", // 活动时间校验
            "whiteListValidate", // 白名单校验
            "regRepeatValidate", // 重复报名校验
            "regQuotaValidate", // 报名人数校验
            "pointsValidate"); // 积分校验

    @Resource
    private ActivityRegService activityRegService;

    @Override
    protected ActivityTypeEnum getActivityType() {
        return ActivityTypeEnum.REG;
    }

    @Override
    protected List<String> getMethods() {
        return methods;
    }

    /**
     * 重复报名校验
     *
     * @author 北渡
     */
    public void regRepeatValidate(PortalActivityRule rule) {
        // 不要求登录
        if (rule.getIsLogin() == 0) {
            return;
        }
        // 已报名
        if (activityRegService.hasRegistered(rule.getActivityId())) {
            throw new ServiceException(ErrorMessage.REG_REPEAT.getMsg());
        }
    }

    /**
     * 报名人数校验
     *
     * @author 北渡
     */
    public void regQuotaValidate(PortalActivityRule rule) {
        // 查询剩余报名名额
        Integer quota = userDataService.queryRegRemainingQuota(rule);
        if (quota < 1) {
            throw new ServiceException(ErrorMessage.REG_QUOTA_FULL.getMsg());
        }
    }

    /**
     * 报名身份校验
     *
     * @author 北渡
     */
    public void regIdentityValidate(PortalActivityRule rule) {
        // 不要求登录
        if (rule.getIsLogin() == 0) {
            return;
        }
        // 未报名
        if (!activityRegService.hasRegistered(rule.getActivityId())) {
            throw new ServiceException(ErrorMessage.REG_NOT_FIND.getMsg());
        }
    }

}
